﻿namespace JenkinsController.Tests.Framework
{
    using System;
    using System.IO;
    using JenkinsController.Framework;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Newtonsoft.Json;
    using Ploeh.AutoFixture;

    [TestClass]
    public class AnimatedConverterTests
    {
        private readonly Fixture defaultFixture = new Fixture();
        private JsonConverter converter;

        [TestInitialize]
        public void InitContext()
        {
            this.converter = new AnimatedConverter();
        }

        [TestMethod]
        [ExpectedException(typeof(NotImplementedException))]
        public void Invoke_WriteJson_ShouldThrowException()
        {
            this.converter.WriteJson(default(JsonWriter), default(object), default(JsonSerializer));
        }

        [TestMethod]
        public void Invoke_ReadJson_WithAnimatedPattern_ShouldReturnTrue()
        {
            var reader = new JsonTextReader(new StringReader("{animated:'yellow_anime'}"));
            // read first element (StartElement token type)
            reader.Read();
            // read animated property name
            reader.Read();
            // read animated property value
            reader.Read();

            Type objectType = typeof(string);
            object existingValue = null;
            var serializer = new JsonSerializer();

            Assert.IsTrue((bool)this.converter.ReadJson(reader, objectType, existingValue, serializer));
        }

        [TestMethod]
        public void Invoke_ReadJson_WithoutAnimatedPattern_ShouldReturnTrue()
        {
            var reader = new JsonTextReader(new StringReader("{animated:'yellow'}"));
            // read first element (StartElement token type)
            reader.Read();
            // read animated property name
            reader.Read();
            // read animated property value
            reader.Read();

            Type objectType = typeof(string);
            object existingValue = null;
            var serializer = new JsonSerializer();

            Assert.IsFalse((bool)this.converter.ReadJson(reader, objectType, existingValue, serializer));
        }

        [TestMethod]
        public void Invoke_ReadJson_WithNullTokenType_ShouldReturnFalse()
        {
            var reader = new JsonTextReader(new StringReader("{animated:null}"));
            // read first element (StartElement token type)
            reader.Read();
            // read animated property name
            reader.Read();
            // read animated property value
            reader.Read();

            Type objectType = typeof(string);
            object existingValue = null;
            var serializer = new JsonSerializer();

            Assert.IsFalse((bool)this.converter.ReadJson(reader, objectType, existingValue, serializer));
        }
    }
}